package com.promise.jokerdream.ui.components

import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.promise.jokerdream.ui.theme.AppFontSizes

/**
 * 任务编辑对话框
 * 用于编辑任务的名称、执行次数和任务链接
 */
@Composable
fun TaskEditDialog(
    show: Boolean,
    taskName: String = "",
    executionCount: String = "",
    taskLink: String = "",
    onDismiss: () -> Unit,
    onConfirm: (String, String, String) -> Unit, // 修改为直接返回三个值
    modifier: Modifier = Modifier
) {
    var localTaskName by remember { mutableStateOf(taskName) }
    var localExecutionCount by remember { mutableStateOf(executionCount) }
    var localTaskLink by remember { mutableStateOf(taskLink) }
    var isValid by remember { mutableStateOf(true) }
    var triggerValidation by remember { mutableStateOf(0) }

    // 状态重置函数
    fun resetState() {
        localTaskName = taskName
        localExecutionCount = executionCount
        localTaskLink = taskLink
        isValid = true
        triggerValidation = 0
    }
    
    // 每次显示时重置状态
    LaunchedEffect(show) {
        if (show) {
            localTaskName = taskName
            localExecutionCount = executionCount
            localTaskLink = taskLink
            isValid = true
            triggerValidation = 0
        }
    }

    PMDialog(
        show = show,
        title = "编辑任务",
        confirmText = "确定",
        cancelText = "取消",
        onDismiss = onDismiss,
        onConfirm = {
            triggerValidation++
            val isTaskNameValid = localTaskName.isNotBlank()
            val isExecutionCountValid = localExecutionCount.isNotBlank() && localExecutionCount.toIntOrNull() != null && localExecutionCount.toInt() > 0
            val isTaskLinkValid = localTaskLink.isNotBlank() && localTaskLink.startsWith("https://")
            
            isValid = isTaskNameValid && isExecutionCountValid && isTaskLinkValid
            
            if (isValid) {
                onConfirm(
                    localTaskName.trim(),
                    localExecutionCount.trim(),
                    localTaskLink.trim()
                )
            }
        }
    ) {
        Column(
            modifier = Modifier.fillMaxWidth(),
            verticalArrangement = Arrangement.spacedBy(16.dp)
        ) {
            // 任务名称输入框
            OutlinedTextField(
                value = localTaskName,
                onValueChange = { 
                    localTaskName = it
                    isValid = it.isNotBlank() && localExecutionCount.isNotBlank() && localTaskLink.isNotBlank()
                },
                label = { Text("任务名称") },
                placeholder = { Text("请输入任务名称") },
                singleLine = true,
                isError = triggerValidation > 0 && localTaskName.isBlank(),
                modifier = Modifier.fillMaxWidth()
            )
            
            // 执行次数输入框
            OutlinedTextField(
                value = localExecutionCount,
                onValueChange = { 
                    localExecutionCount = it
                    val isValidCount = it.isNotBlank() && it.toIntOrNull() != null && it.toInt() > 0
                    isValid = localTaskName.isNotBlank() && isValidCount && localTaskLink.isNotBlank()
                },
                label = { Text("执行次数") },
                placeholder = { Text("请输入执行次数") },
                singleLine = true,
                keyboardOptions = androidx.compose.foundation.text.KeyboardOptions(
                    keyboardType = KeyboardType.Number
                ),
                isError = triggerValidation > 0 && (localExecutionCount.isBlank() || localExecutionCount.toIntOrNull() == null || localExecutionCount.toInt() <= 0),
                modifier = Modifier.fillMaxWidth()
            )
            
            // 任务链接输入框
            OutlinedTextField(
                value = localTaskLink,
                onValueChange = { 
                    localTaskLink = it
                    val isValidLink = it.isNotBlank() && it.startsWith("https://")
                    isValid = localTaskName.isNotBlank() && localExecutionCount.isNotBlank() && isValidLink
                },
                label = { Text("任务链接") },
                placeholder = { Text("输入链接（须以https://开头）") },
                singleLine = true,
                isError = triggerValidation > 0 && (localTaskLink.isBlank() || !localTaskLink.startsWith("https://")),
                modifier = Modifier.fillMaxWidth()
            )
        }
    }
}
